<?php
namespace app\service;
use think\facade\Db;
define("APPID","6A97E0CA77E547EBB7CA897685CD80E3");
define("APPKEY","6DCB7098AF6640C397A70D63D1658EF3");
define("APPSECRET","1FB5F83668104B3290FFA7A893831C54");
define("TimeStamp","2021-01-09 17:07:25");
class wotuService{
    public function personUpdate($personGuid,$arr){

        $url="http://wo-api.uni-ubi.com/v1/".APPID."/person/".$personGuid;
        $tag="";

        if($arr["mzt"]=="绿码"){
            $tag='{"healthCodeColor":1}';
            $tag=json_encode(["healthCodeColor"=>1]);
        }else if($arr["mzt"]=="黄码"){
            $tag='{"healthCodeColor":2}';
            $tag=json_encode(["healthCodeColor"=>2]);
        }else if($arr["mzt"]=="红码"){
            $tag='{"healthCodeColor":3}';
            $tag=json_encode(["healthCodeColor"=>3]);
        }
        $put_data = array(
            "tag"=>$tag,
            "idCardNo"=>$arr["idcard"],
            "cardNo"=> "",
             "name"=> $arr["name"],
             "phone"=> ""
        );
        //设置post数据
        $token = $this->token();
        $header=array(
            'token:'. $token,
            'Content-Type:application/json'
        );
        $data=$this->curl_put_data($url,$header,$put_data);

        $kais = strpos($data, "{");
        $data = substr($data, $kais);
        return $data;
    }
    //根据若人员id查询该人员的授权设备列表
    public function devicelist($personGuid){

        $token=$this->token();
        $header=array(
            'token:'. $token,
            'Content-Type:application/json'
        );
        $url="http://wo-api.uni-ubi.com/v1/".APPID."/person/authorization?personGuid=".$personGuid;
        $data = $this->curl_get_header($url,$header);
        $kais = strpos($data, "{");
        $data = substr($data, $kais);
        $arr = json_decode($data, true);
        return $arr;
    }
    //type:1设备拍照注册，2：IC卡/身份证卡号注册
    public function devreg($deviceKey,$type,$personGuid){
        $appId=APPID;
        $token=$this->token();
        $url="http://gs-api.uface.uni-ubi.com/v1/{$appId}/device/{deviceKey}/mode/state";
        $post_data = array(
            "type"=>$type,
            "personGuid"=>$personGuid,
            "token" => $token,
            "deviceKey"=>$deviceKey    
        );
        $data=$this->curl_post($url,$post_data);
        $kais = strpos($data, "{");
        $data = substr($data, $kais);
        return $data;
    }

    //设备交互模式创建
    public function devopen($deviceKey){
        $url="http://wo-api.uni-ubi.com/v1/".APPID."/device/{$deviceKey}/interactive";
        $token=$this->token();
        $header=array(
            'token:'. $token,
            'Content-Type:application/json'
        );

        $post_data = array(
            "type" => 1
        );
        $data=$this->curl_post($url,$post_data,$header);
        $kais = strpos($data, "{");
        $data = substr($data, $kais);
        $arr = json_decode($data, true);
        return $arr;
        //return  $data;
    }
    
    //注册任务查询
    public function devregquery($personGuid,$deviceKey,$taskId){
        $appid = APPID;
        $token = $this->token();
        $url="http://gs-api.uface.uni-ubi.com/v1/{$appid}/person/{$personGuid}/device/{$deviceKey}/registeration/state/{$taskId}?appId={$appid}&token={$token}&personGuid={$personGuid}&deviceKey={$deviceKey}&taskId={$taskId}";
        $data=$this->curl_get($url);
        $kais = strpos($data, "{");
        $data = substr($data, $kais);
        return $data;
    }
    //注册任务变更
    public function devregupdate($personGuid,$state,$deviceKey,$taskId){
        $appId=APPID;
        $token=$this->token(); 
        $url="http://gs-api.uface.uni-ubi.com/v1/{$appId}/person/{$personGuid}/device/{$deviceKey}/registeration/state/{$taskId}?appId={$appId}&token={$token}&state={$state}&personGuid={$personGuid}&deviceKey={$deviceKey}&taskId={$taskId}";
        $data=$this->curl_put($url);
        $kais = strpos($data, "{");
        $data = substr($data, $kais);
        return $data;
    }
    //人员搜索
    public function people(){
        $appid = APPID;
        $url="http://gs-api.uface.uni-ubi.com/v1/{$appid}/people/search";
        $token = $this->token();
        $post_data = array(
            "token" => $token,
            "idcardNo"=>"",
            "orderFieldKey"=>"6"
        );
        $data=$this->curl_post($url,$post_data);
        $kais = strpos($data, "{");
        $data = substr($data, $kais);
        return $data;
    }
    //设备查询接口
	public function deviceinquiry($devicekey){
        $appid = APPID;
        $token = $this->token();
        //$url = "http://gs-api.uface.uni-ubi.com/v1/{$appid}/device/{$devicekey}?token={$token}";

        $url="http://wo-api.uni-ubi.com/v1/".APPID."/device/{$devicekey}";
        $header=array(
            'token:'. $token,
            'Content-Type:application/json'
        );
        $data = $this->curl_get_header($url,$header);
        $kais = strpos($data, "{");
        $data = substr($data, $kais);
        return $data;
    }

    //人员查询 根据personGuid查询
    public function personinquiry($personGuid){
        $appid = APPID;
        $token = $this->token();
        $url="http://gs-api.uface.uni-ubi.com/v1/{$appid}/person/{$personGuid}?token={$token}";
        //$url="http://gs-api.uface.uni-ubi.com/v1/{$appid}/person/{$personGuid}?appId={$appid}&token={$token}&guid={$personGuid}";
        $data = $this->curl_get($url);
        $kais = strpos($data, "{");
        $data = substr($data, $kais);
        $arr = json_decode($data, true);
        return $arr;
    }
	//人员注册
	public function reg($name,$phone,$cardNo,$idcardNo,$type){

		//$url="http://gs-api.uface.uni-ubi.com/v1/".APPID."/person";
        $url="http://wo-api.uni-ubi.com/v1/".APPID."/person";
        $token = $this->token();
        $header=array(
            'token:'. $token,
            'Content-Type:application/json'
        );
        $tag="业主";
        if($type=="1"){
            $tag="家属";
        }elseif($type=="2"){
            $tag="租户";
        }elseif($type=="3"){
            $tag="访客";
        }elseif($type=="4"){
            $tag="企业员工";
        }elseif($type=="5"){
            $tag="物业员工";
        }
        $tag=$type;
        $post_data = array(
            "name" => $name,	//姓名
            "phone" => $phone,	//手机号码
            "tag"=>$tag,
            "cardNo"=>$cardNo,//卡号（IC卡／身份证等）
            "idcardNo"=>$idcardNo //身份证号
        );
        $data=$this->curl_post($url,$post_data,$header);
        $kais = strpos($data, "{");
        $data = substr($data, $kais);
        //$arr = json_decode($data, true);
        //$info = $arr['data'];
        return  $data;
	}
    //人员删除
    public function del($guid){

        //$url="http://gs-api.uface.uni-ubi.com/v1/".APPID."/person/".$guid."?token={$token}";
        $url="http://wo-api.uni-ubi.com/v1/".APPID."/person?personGuids=".$guid;
        $token = $this->token();
        $header=array(
            'token:'. $token,
            'Content-Type:application/json'
        );

        $data=$this->curl_delete($url,$header);
        $kais = strpos($data, "{");
        $data = substr($data, $kais);
        $arr = json_decode($data, true);
        return $arr;
    }
    //将图片上传到沃土
    public function uploadimg($base64_img, $personGuid)
    {
        $token = $this->token();

        $url="http://wo-api.uni-ubi.com/v1/".APPID."/face";

        $post_data = array(
            "personGuid" => $personGuid,
            "base64" => $base64_img

        );
        $header=array(
            'token:'. $token,
            'Content-Type:application/json'
        );
        $data=$this->curl_post($url,$post_data,$header);
        $kais = strpos($data, "{");
        $data = substr($data, $kais);
        $arr = json_decode($data, true);
        return $arr;
    }
    //将图片(url格式)上传到沃土
    public function uploadimgurl($imgurl, $personGuid)
    {
        $token = $this->token();

        $url="http://wo-api.uni-ubi.com/v1/".APPID."/face";

        $post_data = array(
            "personGuid" => $personGuid,
            "url" => $imgurl

        );
        $header=array(
            'token:'. $token,
            'Content-Type:application/json'
        );
        $data=$this->curl_post($url,$post_data,$header);
        $kais = strpos($data, "{");
        $data = substr($data, $kais);
        $arr = json_decode($data, true);
        return $arr;
    }
    //将uface上的图片删除
    public function delimage($personGuid,$faceGuid){
        $token = $this->token();
        $header=array(
            'token:'. $token,
            'Content-Type:application/json'
        );
        $url="http://wo-api.uni-ubi.com/v1/".APPID."/face/{$faceGuid}?personGuid=".$personGuid;


        $data=$this->curl_delete($url,$header);
        $kais = strpos($data, "{");
        $data = substr($data, $kais);
        $arr = json_decode($data, true);
        return $arr;
    }


    //人员授权设备管理
    public function accredit($personGuid, $deviceKey)
    {
        $type=1;//1:本地库 2:云端库

        $url="http://wo-api.uni-ubi.com/v1/".APPID."/device/{$deviceKey}/person/{$personGuid}/type/{$type}";
        //设置post数据
        $token = $this->token();
        $header=array(
            'token:'. $token,
            'Content-Type:application/json'
        );
        $data=$this->curl_put($url,$header);

        //显示获得的数据
        //    print_r($data);
        $kais = strpos($data, "{");
        $data = substr($data, $kais);
        $arr = json_decode($data, true);
        return $arr;

    }

    //人员销权设备管理
    public function cancelaccredit($personGuid, $deviceKey)
    {
        $type=1;//1:本地库 2:云端库

        $url="http://wo-api.uni-ubi.com/v1/".APPID."/device/{$deviceKey}/person/{$personGuid}/type/{$type}";


        $token = $this->token();
        $header=array(
            'token:'. $token,
            'Content-Type:application/json'
        );
        $data=$this->curl_delete($url,$header);

        //显示获得的数据
        //    print_r($data);
        $kais = strpos($data, "{");
        $data = substr($data, $kais);
        $arr = json_decode($data, true);
        return $arr;

    }

    public function uploadimg2($image, $personGuid)
    {
        $token = $this->token();
        $base64_img = $this->base64EncodeImage($image);
        $url = "http://gs-api.uface.uni-ubi.com/v1/".APPID."/person/{$personGuid}/face";
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 1);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
        $post_data = array(
            "token" => $token,
            "img" => $base64_img,
            "validLevel"=>"0"
        );
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        $kais = strpos($data, "{");
        $data = substr($data, $kais);
        $arr = json_decode($data, true);
        return $arr;
        if (isset($arr['data']['faceUrl'])) {
            $photourl = $arr['data']['faceUrl'];
        } else {
            $photourl = '';
        }
        return $photourl;
    }

    //人员查询
    public function personGuid($personGuid){
        $url="http://wo-api.uni-ubi.com/v1/".APPID."/person/{$personGuid}";
        $token = $this->token();
        $header=array(
            'token:'. $token,
            'Content-Type:application/json'
        );
        $output=$this->curl_get_header($url,$header);
        $kais = strpos($output, "{");
        $data = substr($output, $kais);
        $arr = json_decode($data, true);
        return $arr;
    }
	public function tupian($guid)
    {
        $appid = APPID;
        $token = $this->token();
        $url = "http://gs-api.uface.uni-ubi.com/v1/{$appid}/person/{$guid}/faces?token={$token}";
        $url="";
        $output=$this->curl_get($url);
        $kais = strpos($output, "{");
        $data = substr($output, $kais);
        $arr = json_decode($data, true);
        return $arr;
    }

	public function token()
    {
    	$webinfo= Db::table('webset')->find("1");;
    	$time=time();
        if($time-($webinfo["face_token_time"]/1000)<36000)
        {
            return $webinfo["face_token"];
        }
        else
        {
        	//$url="http://gs-api.uface.uni-ubi.com/v1/".APPID."/auth";
            $url="http://wo-api.uni-ubi.com/v1/".APPID."/auth";
            $time=$this->getMillisecond();
            $str=APPKEY.$time.APPSECRET;
            $sign=strtolower(MD5($str));
            $headdata = array(
                'appKey:'. APPKEY,
                'timestamp:'.$time,
                'sign:'.$sign
            );

            $data=$this->curl_get_header($url,$headdata);
	        $kais = strpos($data, "{");
	        $data = substr($data, $kais);
	        $arr = json_decode($data, true);
	        $info["face_token"]=$arr["data"];;
            $info["face_token_time"]=$time;
            $info["id"]="1";
	        Db::table("webset")
                  ->update($info);
	        return $info["face_token"];
        }
    }
    
	public function curl_post($url,$post_data,$header){
		$curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 1);
        //设置header域
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
        
        curl_setopt($curl, CURLOPT_POSTFIELDS,  json_encode($post_data));
        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        return $data;//json_decode($data, true);
	}
    private function curl_get_header($url,$header){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_HEADER, true);

        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 16);
        curl_setopt($ch, CURLOPT_TIMEOUT, 300);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
    private function curl_get($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1);#我不需要获取头部啊;
        
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 16);
        curl_setopt($ch, CURLOPT_TIMEOUT, 300);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
    public function curl_put($url,$header){
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 1);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //设置post方式提交
        //curl_setopt($curl, CURLOPT_PUT, 1);
        $method="PUT";
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); //设置请求方式
     
        curl_setopt($curl,CURLOPT_HTTPHEADER,$header);//设置HTTP头信息
        //curl_setopt($curl, CURLOPT_PUT, $post_data);
        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        return $data;
        
    }
    public function curl_put_data($url,$header,$put_data){
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 1);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //设置post方式提交
        //curl_setopt($curl, CURLOPT_PUT, 1);
        $method="PUT";
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); //设置请求方式

        curl_setopt($curl,CURLOPT_HTTPHEADER,$header);//设置HTTP头信息
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($put_data));
        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        return $data;

    }
    public function curl_delete($url,$header){
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 1);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //设置delete方式提交

        $method="DELETE";
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); //设置请求方式
        curl_setopt($curl,CURLOPT_HTTPHEADER,$header);//设置HTTP头信息

        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        return $data;

    }
    public function base64EncodeImage($image_file)
    {
        $base64_image = '';
        $image_info = getimagesize($image_file);
        $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
        $base64_image = chunk_split(base64_encode($image_data));
        return $base64_image;
    }
    
    public function urlsafe_b64decode($string) {
       $data = str_replace(array('-','_'),array('+','/'),$string);
       $mod4 = strlen($data) % 4;
       if ($mod4) {
           $data .= substr('====', $mod4);
       }
       return $data;
       return base64_decode($data);
    }

    //获取当前毫秒时间戳
    public function getMillisecond()
    {
        //获取毫秒的时间戳
        $time = explode(" ", microtime());
        $time = $time[1] . ($time[0] * 1000);
        $time2 = explode(".", $time);
        $time = $time2[0];
        return $time;
    }
}